package utils.entity;

import org.apache.commons.lang3.StringUtils;
import utils.FileUtil;
import utils.Log;
import utils.ProcessExecutor;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class Device {
    private static final String TAG = "Device";
    private String ip = "127.0.0.1";
    private String sn = "";
    private static final ProcessExecutor PROCESS_EXECUTOR = new ProcessExecutor();

    public Device(String sn) {
        this.sn = sn;
    }

    public String getSn() {
        return sn;
    }

    @Override
    public String toString() {
        return sn;
    }

    public boolean isOnline() {
        String result = PROCESS_EXECUTOR.callProcess("hdc list targets");
        return !"".equals(sn) && result.contains(sn);
    }


    public BufferedImage getScreenshot(File saveFile) {
        try {
            PROCESS_EXECUTOR.callProcess(String.format("hdc -t %s shell rm /data/local/tmp/screen.jpeg", sn), null, 7);
            PROCESS_EXECUTOR.callProcess(String.format("hdc -t %s shell snapshot_display -f /data/local/tmp/screen.jpeg", sn), null, 7);
            if (saveFile == null) {
                File tempDirectory = new File(FileUtil.getTempDirectory());
                if (!tempDirectory.exists()) {
                    tempDirectory.mkdirs();
                }
                String jpegName = String.format("%s-screen.jpeg", sn);
                saveFile = new File(FileUtil.getTempDirectory(), jpegName);
            }
            String pullCmd = String.format("hdc -t %s file recv /data/local/tmp/screen.jpeg \"%s\"", sn, saveFile.getCanonicalPath());
            String pullResult = PROCESS_EXECUTOR.callProcess(pullCmd);
            Log.debug(TAG, "pull screenshot result: " + pullResult);
            if (!saveFile.exists()) {
                Log.info(TAG, "screenshot file not exist");
                return null;
            }
            return ImageIO.read(saveFile);
        } catch (Exception ex) {
            Log.error(TAG, "get screenshot fail", ex);
        }
        return null;
    }

    public String getLayout(File saveFile) {
        try {
            String result = PROCESS_EXECUTOR.callProcess(String.format("hdc -t %s shell uitest dumpLayout", sn), null, 7);
            Log.debug(TAG, "dump layout result: " + result);
            if (StringUtils.isBlank(result) || !result.contains("DumpLayout saved to:")) {
                Log.debug(TAG, "dump layout failed: " + result);
                return null;
            }
            int first = result.lastIndexOf("to:") + 3;
            int last = result.indexOf(".json") + 6;
            String savePath = result.substring(first, last).trim();
            if (saveFile == null) {
                File tempDirectory = new File(FileUtil.getTempDirectory());
                if (!tempDirectory.exists()) {
                    tempDirectory.mkdirs();
                }
                saveFile = new File(FileUtil.getTempDirectory(), "dumpLayout.json");
            }

            result = PROCESS_EXECUTOR.callProcess(String.format("hdc -t %s file recv \"%s\" \"%s\"", sn, savePath, saveFile.getCanonicalPath()));
            if (!result.contains("finish") || !saveFile.exists()) {
                Log.info(TAG, "pull dumpLayout.json fail");
                return null;
            }
            return FileUtil.readFileContent(saveFile);
        } catch (Exception ex) {
            Log.error(TAG, "get layout fail", ex);
            return null;
        }
    }
}
